package com.lefevre.cms.bean.question;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 问题
 *
 * @author Lefevre
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("question")
@Entity
@Table(indexes = {@Index(name = "question_1_idx", columnList = "userName,postTime"), @Index(name = "question_4_idx", columnList = "status,sort,lastAnswerTime"), @Index(name = "question_5_idx", columnList = "adoptionAnswerId,status,sort,lastAnswerTime"), @Index(name = "question_6_idx", columnList = "point,status,sort,lastAnswerTime"), @Index(name = "question_7_idx", columnList = "amount,status,sort,lastAnswerTime")})
public class Question implements Serializable {
    private static final long serialVersionUID = 8441186002971301170L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @TableId(type = IdType.INPUT)
    private Long id;

    /**
     * 标题
     **/
    @Column(length = 190)
    @TableField("`title`")
    private String title;

    /**
     * 标签
     **/
    @Transient
    @TableField(exist = false)
    private List<QuestionTagAssociation> questionTagAssociationList = new ArrayList<>();

    /**
     * 问题内容
     **/
    @Lob
    @TableField("`content`")
    private String content;

    /**
     * 内容摘要
     **/
    @Lob
    @TableField("`summary`")
    private String summary;

    /**
     * 追加内容
     **/
    @Lob
    @TableField("appendContent")
    private String appendContent = "[";

    /**
     * 追加内容集合
     **/
    @Transient
    @TableField(exist = false)
    private List<AppendQuestionItem> appendQuestionItemList = new ArrayList<>();

    /**
     * 发表时间
     **/
    @Temporal(TemporalType.TIMESTAMP)
    @TableField("postTime")
    private Date postTime = new Date();

    /**
     * 最后回答时间
     **/
    @Temporal(TemporalType.TIMESTAMP)
    @TableField("lastAnswerTime")
    private Date lastAnswerTime;

    /**
     * 最后修改时间
     **/
    @TableField("lastUpdateTime")
    @Temporal(TemporalType.TIMESTAMP)
    private Date lastUpdateTime;

    /**
     * IP
     **/
    @Column(length = 45)
    @TableField("ip")
    private String ip;

    /**
     * IP归属地
     **/
    @Transient
    @TableField(exist = false)
    private String ipAddress;

    /**
     * 采纳的答案Id
     **/
    @TableField("adoptionAnswerId")
    private Long adoptionAnswerId = 0L;

    /**
     * 答案总数
     **/
    @TableField("answerTotal")
    private Long answerTotal = 0L;

    /**
     * 允许回答
     **/
    @TableField("allow")
    private boolean allow = true;

    /**
     * 查看总数
     **/
    @TableField("viewTotal")
    private Long viewTotal = 0L;

    /**
     * 用户名称
     **/
    @Column(length = 30)
    @TableField("userName")
    private String userName;

    /**
     * 呢称
     **/
    @Transient
    @TableField(exist = false)
    private String nickname;

    /**
     * 头像路径
     **/
    @Transient
    @TableField(exist = false)
    private String avatarPath;

    /**
     * 头像名称
     **/
    @Transient
    @TableField(exist = false)
    private String avatarName;

    /**
     * 用户角色名称集合
     **/
    @Transient
    @TableField(exist = false)
    private List<String> userRoleNameList = new ArrayList<>();

    /**
     * 是否为员工 true:员工  false:会员
     **/
    @TableField("isStaff")
    private Boolean isStaff = false;

    /**
     * 排序
     **/
    @TableField("sort")
    private Integer sort = 0;

    /**
     * 状态 10.待审核 20.已发布 110.待审核删除 120.已发布删除
     **/
    @TableField("status")
    private Integer status = 10;

    /**
     * 悬赏金额
     **/
    @Column(nullable = false, precision = 12, scale = 2)
    @TableField(value = "amount", numericScale = "2")
    private BigDecimal amount = new BigDecimal("0.00");

    /**
     * 悬赏积分
     **/
    @TableField("point")
    private Long point = 0L;

    /**
     * 添加标签
     */
    public void addQuestionTagAssociation(QuestionTagAssociation questionTagAssociation) {
        this.getQuestionTagAssociationList().add(questionTagAssociation);
    }

}
